package cn.com.shinho.hg.auth.modular.provider;

import cn.com.shinho.hg.auth.modular.entity.TbSysSecret;
import cn.com.shinho.hg.auth.modular.service.ISysPermissionService;
import cn.com.shinho.hg.auth.modular.service.ISysSecretService;
import cn.com.shinho.hg.auth.modular.service.ISysUserService;
import cn.com.shinho.hg.core.base.controller.BaseController;
import cn.com.shinho.hg.core.base.response.ResponseData;
import cn.com.shinho.hg.core.base.response.SuccessResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户中心接口实现
 *
 * @author Mr.Ye
 * @date 2018年06月09日19:38:34
 */
@RestController
@RequestMapping("/api/authService")
public class AuthContrller extends BaseController{

    @Autowired
    private ISysPermissionService sysPermissionService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysSecretService secretService;

    /**
     * 根据用户id获取用户信息
     *
     * @author Mr.Ye
     */
    @GetMapping("/getUser/{userId}")
    @ResponseBody
    public ResponseData getUserById(@PathVariable("userId") Long userId) {
        return SuccessResponseData.success(sysUserService.getUserLoginInfo(userId));
    }

    /**
     * 获取用户的权限路径
     *
     * @author Mr.Ye
     */
    @GetMapping("/getPermissionUrls/{userId}")
    @ResponseBody
    public ResponseData getUserPermissionUrls(@PathVariable("userId") Long userId) {
        return SuccessResponseData.success(sysPermissionService.getPermissionUrlsByUserId(userId));
    }

    /**
     * 通过appId获取secret
     *
     * @author Mr.Ye
     */
    @GetMapping("/getSecret/{appId}")
    @ResponseBody
    public ResponseData getSecretByAppId(@PathVariable("appId") String appId) {

        List<TbSysSecret> secretsList = secretService.getSecretsList(appId);
        if (secretsList != null && !secretsList.isEmpty()) {
            return SuccessResponseData.success(secretsList.get(0).getSecret());
        } else {
            return SuccessResponseData.success(null);
        }
    }

    /**
     * 通过appId获取secret
     *
     * @author Mr.Ye
     * @Date 2018/05/12 23:20
     */
    @GetMapping("/testPage")
    @ResponseBody
    public ResponseData testPage() {
        return  SuccessResponseData.success(sysUserService.selectAllUser());
    }
}